package avicit.im.lantop.basic.archivetemplatemanager.rest;


import avicit.im.lantop.basic.archivetemplatemanager.service.ArchivetemplatemanagerService;
import avicit.im.lantop.basic.archivetemplatemanager.service.api.ArchivetemplatemanagerServiceApi;
import avicit.im.lantop.basic.companycorrespondence.rest.CompanyCorrespondenceServiceRest;
import avicit.platform6.core.dao.Paging;
import avicit.platform6.core.rest.msg.ResponseMsg;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "ArchivetemplatemanagerService", description = "ArchivetemplatemanagerService")
@RequestMapping("/api/avicit/im/lantop/basic/archivetemplatemanager/rest/ArchivetemplatemanagerServiceRest")
public class ArchivetemplatemanagerServiceRest {


    private static final Logger LOGGER = LoggerFactory.getLogger(ArchivetemplatemanagerServiceRest.class);


    @Resource
    private ArchivetemplatemanagerService archivetemplatemanagerService;


    /**
     * 获取归档模板信息
     * @param map
     * @return
     */
    @PostMapping("/getUserIdsAuthDatasByRecord")
    @ApiOperation(value = "获取归档模板信息")
    public ResponseMsg<List<Map<String, Object>>> getUserIdsAuthDatasByRecord(@ApiParam(value = "获取归档模板信息", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<List<Map<String, Object>>> responseMsg = new ResponseMsg<>();
        try {
            String project = (String) map.get("project");
            String orgId = (String) map.get("orgId");
            List<Map<String, Object>> maps = archivetemplatemanagerService.selectPlan(project, orgId);
            responseMsg.setResponseBody(maps);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


    /**
     * 查询计划下所有项目信息
     * @param map
     * @return
     */
    @PostMapping("/selectProject")
    @ApiOperation(value = "查询计划下所有项目信息")
    public ResponseMsg<List<Map<String, Object>>> selectProject(@ApiParam(value = "查询计划下所有项目信息", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<List<Map<String, Object>>> responseMsg = new ResponseMsg<>();
        try {
            String planName = (String) map.get("planName");
            List<Map<String, Object>> maps = archivetemplatemanagerService.selectProject(planName);
            responseMsg.setResponseBody(maps);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


    /**
     * 更新项目信息
     * @param map
     * @return
     */
    @PostMapping("/updateProjct")
    @ApiOperation(value = "更新项目信息")
    public ResponseMsg<Map<String, Object>> updateProjct(@ApiParam(value = "更新项目信息", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();
        try {
            String planId = (String) map.get("planId");
            String planNameto = (String) map.get("planNameto");
            Map<String, Object> modelAndView = archivetemplatemanagerService.updateProjct(planId, planNameto);
            responseMsg.setResponseBody(modelAndView);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


    /**
     * 新增项目信息
     * @param map
     * @return
     */
    @PostMapping("/saveProject")
    @ApiOperation(value = "新增项目信息")
    public ResponseMsg<Map<String, Object>> saveProject(@ApiParam(value = "新增项目信息", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();
        try {
            String orgId = (String) map.get("orgId");
            String deptId = (String) map.get("deptId");
            String sysId = (String) map.get("sysId");
            String projectNameNew = (String) map.get("projectNameNew");
            String commitTypeto = (String) map.get("commitTypeto");
            String planId = (String) map.get("planId");
            Integer count = (Integer) map.get("count");
            Map<String,Object> modelAndView = archivetemplatemanagerService.saveProject(orgId, deptId, sysId, projectNameNew, commitTypeto, planId, count);
            responseMsg.setResponseBody(modelAndView);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


    /**
     * 删除计划信息
     * @param map
     * @return
     */
    @PostMapping("/deletePlan")
    @ApiOperation(value = "删除计划信息")
    public ResponseMsg deletePlan(@ApiParam(value = "删除计划信息", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg responseMsg = new ResponseMsg<>();
        try {
            String idVal = (String) map.get("idVal");
            archivetemplatemanagerService.deletePlan(idVal);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


    /**
     * 新增归档模板计划
     * @param map
     * @return
     */
    @PostMapping("/savePlan")
    @ApiOperation(value = "新增归档模板计划")
    public ResponseMsg<Map<String, Object>> savePlan(@ApiParam(value = "新增归档模板计划", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();
        try {
            String orgId = (String) map.get("orgId");
            String deptId = (String) map.get("deptId");
            String sysId = (String) map.get("sysId");
            String planName = (String) map.get("planName");
            String type = (String) map.get("type");
            String o = (String) map.get("o");
            String i = (String) map.get("i");
            Map<String, Object> map1 = archivetemplatemanagerService.savePlan(orgId, deptId, sysId, planName, type, null, 0);
            responseMsg.setResponseBody(map1);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


    /**
     *
     * @param map
     * @return
     */
    @PostMapping("/selectProjectList")
    @ApiOperation(value = "新增归档模板计划")
    public ResponseMsg<Map<String, Object>> selectProjectList(@ApiParam(value = "新增归档模板计划", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();
        try {
            String id = (String) map.get("id");
            Integer page = (Integer) map.get("page");
            Integer rows = (Integer) map.get("rows");
            Map<String, Object> map1 = archivetemplatemanagerService.selectProjectList(id, page, rows);
            responseMsg.setResponseBody(map1);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


}
